package com.nidu.demo.web.system;

import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.Response;
import com.nidu.demo.cache.preload.CachePreloadManager;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "管理后台 - 缓存")
@RestController
@RequestMapping("/system/cache")
@RequiredArgsConstructor
public class SysCacheController {

    private final CachePreloadManager cachePreloadManager;

    @GetMapping("/preloads")
    public MultiResponse<String> getPreloads() {
        List<String> preloadNames = cachePreloadManager.getPreloaderNames();
        return MultiResponse.of(preloadNames);
    }

    @PostMapping("/preloads")
    public Response preloadCaches() {
        cachePreloadManager.reloadAllCaches();
        return Response.buildSuccess();
    }

    @PostMapping("/preload/{name}")
    public String preloadCache(@PathVariable String name) {
        cachePreloadManager.preloadCache(name);
        return "缓存预热完成: " + name;
    }


}